<?php
  class Paginator

  {

      var $items_per_page;

      var $items_total;

      var $current_page;

      var $num_pages;

      var $mid_range;

      var $low;

      var $high;

      var $limit;

      var $return;

      var $default_ipp;

      var $querystring;

      var $skip_arr = array('pg', 'ipp', 'updated', 'added', 'deleted', 'padded');
	      

      /**

       * Paginator::__construct()

       * 

       * @return

       */

      function __construct()

      {

          $this->current_page = 1;

          $this->mid_range = 7;

          $this->items_per_page = (!empty($_GET['ipp'])) ? $_GET['ipp'] : $this->default_ipp;

      }

      

      /**

       * Paginator::paginate()

       * 

       * @param bool $path

       * @return

       */

      function paginate($path = false){
			global $config;
		
       if (!is_numeric($this->items_per_page) || $this->items_per_page <= 0)

        $this->items_per_page = $this->default_ipp;

       $this->num_pages = ceil($this->items_total / $this->items_per_page);

          

          $this->current_page = intval($_REQUEST['pg']);

          if ($this->current_page < 1 or !is_numeric($this->current_page))

              $this->current_page = 1;

          if ($this->current_page > $this->num_pages)

              $this->current_page = $this->num_pages;

          $prev_page = $this->current_page - 1;

          $next_page = $this->current_page + 1;

          

          if ($_GET) {

              $args = explode("&", $_SERVER['QUERY_STRING']);

              foreach ($args as $arg) {

                  $keyval = explode("=", $arg);

                  if (!in_array($keyval[0], $this->skip_arr))

                      $this->querystring .= "&amp;" . $arg;

              }

          }

          

          if ($_POST) {

              foreach ($_POST as $key => $val) {

                  if ( !in_array($key, $this->skip_arr) )

                      $this->querystring .= "&amp;$key=$val";

              }

          }
        
		  //echo $this->querystring;
		  
          if ($this->num_pages > 1) {

              if ($this->current_page != 1 && $this->items_total >= 10) {

                  if ($path) {

                      $this->return = "<a href=\"" . $config['baseurl'] . "/$path-$prev_page.html\">&laquo; Prev</a>";

                  } else {

                      $this->return = "<a href=\"$_SERVER[PHP_SELF]?pg=$prev_page&amp;ipp=$this->items_per_page$this->querystring\">&laquo; Prev</a>";

                  }

              } else {

                   //$this->return = "<a href=\"$_SERVER[PHP_SELF]?pg=$prev_page&amp;ipp=$this->items_per_page$this->querystring\">&laquo;Prev</a>";
				   $this->return = "&laquo;Prev&nbsp;";

              }



              

              $this->start_range = $this->current_page - floor($this->mid_range / 2);

              $this->end_range = $this->current_page + floor($this->mid_range / 2);

              

              if ($this->start_range <= 0) {

                  $this->end_range += abs($this->start_range) + 1;

                  $this->start_range = 1;

              }

              if ($this->end_range > $this->num_pages) {

                  $this->start_range -= $this->end_range - $this->num_pages;

                  $this->end_range = $this->num_pages;

              }

              $this->range = range($this->start_range, $this->end_range);

              

              for ($i = 1; $i <= $this->num_pages; $i++) {

                  if ($this->range[0] > 2 && $i == $this->range[0])

                      $this->return .= " ... ";

					  

                  if ($i == 1 or $i == $this->num_pages or in_array($i, $this->range)) {

                      if ($i == $this->current_page) {

                          $this->return .= "<a href=\"#\"   class=\"number current\">$i</a>";

                      } else {

                          if ($path) {

                              $this->return .= "<a class=\"number\" title=\" $i - $this->num_pages\" href=\"" . $config['baseurl'] . "/$path-$i.html\">$i</a>";

                          } else {

                              $this->return .= "<a class=\"number\"  href=\"$_SERVER[PHP_SELF]?pg=$i&amp;ipp=$this->items_per_page$this->querystring\">$i</a>";

                          }

                      }

                  }

                  if ($this->range[$this->mid_range - 1] < $this->num_pages - 1 && $i == $this->range[$this->mid_range - 1])

                      $this->return .= " ... ";

              }
              if ($this->current_page != $this->num_pages && $this->items_total >= 10) {

                  if ($path) {

                      $this->return .= "<a href=\"" . $config['baseurl'] . "/$path-$next_page.html\">Next &raquo;</a>\n";

                  } else {
						
                      $this->return .= "<a href=\"$_SERVER[PHP_SELF]?pg=$next_page&amp;ipp=$this->items_per_page$this->querystring\">Next &raquo;</a>\n";

                  }

              } else {
			  	 if($next_page > $this->num_pages){
				 		$next_page	 = $this->num_pages ;
				 }
                   //$this->return .= "<a href=\"$_SERVER[PHP_SELF]?pg=$next_page&amp;ipp=$this->items_per_page$this->querystring\">Next &raquo;</a>\n";
				   $this->return .= "Next &raquo;\n";

              }

          } else {
				
              for ($i = 1; $i <= $this->num_pages; $i++) {

                  if ($i == $this->current_page) {

                      $this->return .= "<a href=\"#\" class=\"current\">$i</a>";

                  } else {
						
                      $this->return .= "<a class=\"number\" href=\"$_SERVER[PHP_SELF]?pg=$i&amp;ipp=$this->items_per_page$this->querystring\">$i</a>";

                  }

              }

          }

          $this->low = ($this->current_page - 1) * $this->items_per_page;

          $this->high = $this->current_page * $this->items_per_page - 1;

          $this->limit = " LIMIT $this->low,$this->items_per_page";

      }

  
      /**

       * Paginator::paginate()

       * 

       * @param bool $path

       * @return

       */

      function adminPaginate($path = false)
       {



       if (!is_numeric($this->items_per_page) || $this->items_per_page <= 0)

        $this->items_per_page = $this->default_ipp;

       $this->num_pages = ceil($this->items_total / $this->items_per_page);

          

          $this->current_page = intval($_REQUEST['pg']);

          if ($this->current_page < 1 or !is_numeric($this->current_page))

              $this->current_page = 1;

          if ($this->current_page > $this->num_pages)

              $this->current_page = $this->num_pages;

          $prev_page = $this->current_page - 1;

          $next_page = $this->current_page + 1;

          

          if ($_GET) {

              $args = explode("&", $_SERVER['QUERY_STRING']);

              foreach ($args as $arg) {

                  $keyval = explode("=", $arg);

                  if (!in_array($keyval[0], $this->skip_arr))

                      $this->querystring .= "&amp;" . $arg;

              }

          }

          

          if ($_POST) {

              foreach ($_POST as $key => $val) {

                  if ( ! in_array($key, $this->skip_arr) )

                      $this->querystring .= "&amp;$key=$val";

              }

          }

          

          if ($this->num_pages > 1) {

              if ($this->current_page != 1 && $this->items_total >= 10) {

                  if ($path) {

                 $this->return = "<a href=\"" . SITEURL . "/admin/$path&pg=$prev_page&amp;ipp=$this->items_per_page\">&laquo; Prev</a>";

                  } else {

                      $this->return = "<a href=\"" . SITEURL . "/admin/$path&pg=$prev_page&amp;ipp=$this->items_per_page\">&laquo;Prev</a>";

                  }

              } else {

                   $this->return = "<a href=\"" . SITEURL . "/admin/$path&pg=$prev_page&amp;ipp=$this->items_per_page\">&laquo; Prev</a>";

              }

              

              $this->start_range = $this->current_page - floor($this->mid_range / 2);

              $this->end_range = $this->current_page + floor($this->mid_range / 2);

              

              if ($this->start_range <= 0) {

                  $this->end_range += abs($this->start_range) + 1;

                  $this->start_range = 1;

              }

              if ($this->end_range > $this->num_pages) {

                  $this->start_range -= $this->end_range - $this->num_pages;

                  $this->end_range = $this->num_pages;

              }

              $this->range = range($this->start_range, $this->end_range);

              

              for ($i = 1; $i <= $this->num_pages; $i++) {

                  if ($this->range[0] > 2 && $i == $this->range[0])

                      $this->return .= " ... ";

					  

                  if ($i == 1 or $i == $this->num_pages or in_array($i, $this->range)) {

                      if ($i == $this->current_page) {

                          $this->return .= "<a href=\"#\" title=\"".lang('GOTO_PAGE')." $i - $this->num_pages\" class=\"number current\">$i</a>";

                      } else {
                          if ($path) {

				 $this->return .= "<a  class=\"number\" title=\"".lang('GOTO_PAGE')." $i - $this->num_pages\" href=\"" . SITEURL . "/admin/$path&pg=$i&amp;ipp=$this->items_per_page\">$i</a>";
                          } else {

				$this->return .= "<a  class=\"number\" title=\"".lang('GOTO_PAGE')." $i - $this->num_pages\" href=\"" . SITEURL . "/admin/$path&pg=$i&amp;ipp=$this->items_per_page\">$i</a>";

                          }

                      }

                  }

                  if ($this->range[$this->mid_range - 1] < $this->num_pages - 1 && $i == $this->range[$this->mid_range - 1])

                      $this->return .= " ... ";

              }

              if ($this->current_page != $this->num_pages && $this->items_total >= 10) {


                  if ($path) {

                      $this->return .= "<a href=\"" . SITEURL . "/admin/$path&pg=$next_page&amp;ipp=$this->items_per_page\">&laquo; Next</a>";

                  } else {

                     $this->return .= "<a href=\"" . SITEURL . "/admin/$path&pg=$next_page&amp;ipp=$this->items_per_page\">&laquo; Next</a>";

                  }

              } else {

                   $this->return .= "<a href=\"" . SITEURL . "/admin/$path&pg=$next_page&amp;ipp=$this->items_per_page\">&laquo; Next</a>";

              }

          } else {

              for ($i = 1; $i <= $this->num_pages; $i++) {

                  if ($i == $this->current_page) {

                      $this->return .= "<a href=\"#\" class=\"current\">$i</a>";

                  } else {

                      $this->return .= "<a class=\"number\" href=\"$_SERVER[PHP_SELF]?pg=$i&amp;ipp=$this->items_per_page$this->querystring\">$i</a>";

                  }

              }

          }

          $this->low = ($this->current_page - 1) * $this->items_per_page;

          $this->high = $this->current_page * $this->items_per_page - 1;

          $this->limit = " LIMIT $this->low,$this->items_per_page";

      }

    

      /**

       * Paginator::display_pages()

       * 

       * @return

       */

      function display_pages()

      {

          return ($this->items_total > $this->default_ipp) ? $this->return : "";

      }
	  
	  function display_page_limits()
	  {
		  
		   if($this->items_total < 5) 
		   {
				return '';   
			}
		  
		  if ($_GET) {
             $args = explode("&", $_SERVER['QUERY_STRING']);
             foreach ($args as $arg) {
                  $keyval = explode("=", $arg);
                  if ( ! in_array($keyval[0], $this->skip_arr) && $keyval[0] != 'new_page_limit')
                     $querystring .= $arg."&amp;" ;
	             }
	         }

          if ($_POST) {
             foreach ($_POST as $key => $val) {
                  if ( ! in_array($key, $this->skip_arr) && $key != 'new_page_limit')
                      $querystring .= "&amp;$key=$val";
             }
          }
		  
		 $querystring = substr($querystring, 0, -5 );
		 $location = $_SERVER['PHP_SELF'].'?'.$querystring;
		 
		 
		 if(preg_match("/(players.php)|(domestic-players.html)/", $_SERVER['PHP_SELF']))
		 {
			$option_array = array('5'=>5, '10'=>10, '15'=>15, '20'=>20, '25'=>25, '30'=>30, '50'=>50, '100'=>100 );
		 }
		 else
		 {
			$option_array = array('5'=>5, '10'=>10, '15'=>15, '20'=>20, '25'=>25, '30'=>30, '50'=>50, '100'=>100, 'all'=> 'All' );	 
		 }
		 
		 
		 $option = '';
		 foreach( $option_array as $key=>$val)
		 {
			 $selected = "";	 
			 if( isset($_REQUEST['new_page_limit']) && $_REQUEST['new_page_limit'] == $key){
				$selected = "selected='selected'";	 
			 } elseif($this->default_ipp == $key) {
				$selected = "selected='selected'";
			 }
			 $option .= '<option '.$selected.' value="'.$key.'"> '.$val.' </option>';
		 }

		 $select = ' <strong> Display </strong><select class="page_limit_select" name="new_page_limit" onchange="location.href=\''.$location.'&new_page_limit=\'+this.value"> 
					'.$option.'
            </select>';  
			return $select;
		  
	  }

  }

?>